<?php
$url = $this->url;
$separator = $this->separator;
$get = $this->get;
$disable_price = $this->disable_price;
$disabled_facet = (array) $this->disabled_facet;
?>
<form>
    <fieldset>
    <?php //echo '<pre>';print_r($this->facet_counts->facet_fields);exit;?>
    <?php //echo '<pre>';print_r($this->facet_queries);exit;?>
        <?php foreach($this->facet_counts->facet_fields as $name => $facet):?>
            <?php $facet = (array) $facet;?>
                <?php if ($name == 'category_l1' && !empty($facet)):?>
                    <div class="link">
                    <label>类别</label>
                    <?php if(empty($get['small_category_list']) || strlen($get['small_category_list']) != 24):?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = Model_Category::find($id);?>
                            <?php /* 
                                if (empty($cat)) {
                                    echo "category not found: $id";
                                }
                                */
                                $url1 = preg_replace('#&small_category_list=[^&]*#', '', $url);
                            ?>
                            <a href="<?php echo $url1 . $separator . 'small_category_list=' . $id;?>"><span><?php echo $cat->name;?>(<?php echo $num?>)</span></a>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php 
                            $cat = Model_Category::find($get['small_category_list']);
                            $url1 = preg_replace('#(\?.*)#', '', $url);
                            //$url1 .= (!empty($get['view'])) ? '?view='.$get['view'] : '';
                            $url1 .= preg_replace('#(\?|&)view=[^&]*#', '', $url1);
                        ?>
                        <span><?php echo $cat->name;?><a href="<?php echo $url1?>">[X]</a></span>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if (!in_array('category_l2', $disabled_facet) && $name == 'category_l2' && !empty($facet) && !empty($get['small_category_list'])):?>
                    <div class="link">
                    <label>小类</label>
                    <?php if(empty($get['category_attached'])):?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = Model_Category::find($id);?>
                            <?php
                                if($id == '_empty_' || $cat->display_website != 1) {
                                    continue;
                                }
                                $url1 = parse_url($url, PHP_URL_PATH) . '?'.http_build_query(array(
                                    'small_category_list' => $get['small_category_list'],
                                    'category_attached' => $id,
                                ));
                            ?>
                            <a href="<?php echo $url1;?>"><?php echo $cat->name;?>(<?php echo $num?>)</a>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php 
                            $cat = Model_Category::find($get['category_attached']);
                            $url1 = parse_url($url, PHP_URL_PATH) . '?'.http_build_query(array(
                                'small_category_list' => $get['small_category_list'],
                            ));
                            
                        ?>
                        <span><?php echo $cat->name;?><a href="<?php echo $url1?>">[X]</a>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'city' && !empty($facet)  && count($facet)>0 && !empty($get['small_category_list'])):?>
                    <?php
                        if(!empty($get['area_l1'])) {
                            $loc = Model_Location::find($get['area_l1']);
                            $get['city'] = (string)$loc->city;
                        }
                        if(!empty($get['area_l2'])) {
                            $loc = Model_Location::find($get['area_l2']);
                            $get['area_l1'] = (string)$loc->area_l1->_id;
                            $get['city'] = (string)$loc->area_l1->city;
                        }
                    ?>
                    <div class="link">
                    <label>城市</label>
                    <?php if(empty($get['city'])):?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = Model_Location::getCityName($id);?>
                            <a href="<?php echo preg_replace('#&city=[^&]*#', '', $url) . $separator . 'city=' . $id;?>"><?php echo $cat;?>(<?php echo $num?>)</a>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $cat = Model_Location::getCityName($get['city']);?>
                        <?php $url1 = preg_replace('#&area_l2=[^&]*#', '', $url)?>
                        <?php $url1 = preg_replace('#&area_l1=[^&]*#', '', $url1)?>
                        <span><?php echo $cat;?><a href="<?php echo preg_replace('#&city=[^&]*#', '', $url1) . $separator . 'city=0' ?>">[X]</a>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'area_l1' && !empty($facet)  && count($facet)>0 && (!empty($get['city']) || !empty($get['area_l2'])) && !empty($get['small_category_list'])):?>
                    <?php 
                        if(!empty($get['area_l2'])) {
                            $loc = Model_Location::find($get['area_l2']);
                            $get['area_l1'] = (string)$loc->area_l1->_id;
                        }
                    ?>
                    <div class="link">
                    <label>区域</label>
                    <?php if(empty($get['area_l1'])):?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = Model_Location::find($id);?>
                            <a href="<?php echo preg_replace('#&area_l1=[^&]*#', '', $url) . $separator . 'area_l1=' . $id;?>"><?php echo $cat->name;?>(<?php echo $num?>)</a>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $cat = Model_Location::find($get['area_l1']);?>
                        <?php $url1 = preg_replace('#&area_l2=[^&]*#', '', $url)?>
                        <span><?php echo $cat->name;?><a href="<?php echo preg_replace('#&area_l1=[^&]*#', '', $url1) . $separator . 'city=' . $cat->city;?>">[X]</a>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'area_l2' && !empty($facet) && (!empty($get['area_l1']) || !empty($get['area_l2'])) && count($facet)>0 && !empty($get['small_category_list'])):?>
                    <div class="link">
                    <label></label>
                    <?php if(empty($get['area_l2'])):?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = Model_Location::find($id);?>
                            <?php $url1 = preg_replace('#&area_l1=[^&]*#', '', $url)?>
                            <?php $url1 = preg_replace('#&city=[^&]*#', '', $url1)?>
                            <a href="<?php echo preg_replace('#&area_l2=[^&]*#', '', $url1) . $separator . 'area_l2=' . $id;?>"><?php echo $cat->name;?>(<?php echo $num?>)</a>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $cat = Model_Location::find($get['area_l2']);?>
                        <span><?php echo $cat->name;?><a href="<?php echo preg_replace('#&area_l2=[^&]*#', '', $url) . $separator . 'area_l1=' . $cat->area_l1->_id;?>">[X]</a>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'make' && !empty($facet) && count($facet)>0 && !empty($get['small_category_list']) && !empty($get['category_attached'])):?>
                    <div class="link">
                    <label>品牌</label>
                    <?php if(empty($get['make'])):?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = Model_Listing_Attribute::find($id);?>
                            <a href="<?php echo preg_replace('#&make=[^&]*#', '', $url) . $separator . 'make=' . $id;?>"><?php echo $cat->name;?>(<?php echo $num?>)</a>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $cat = Model_Listing_Attribute::find($get['make']);?>
                        <span><?php echo $cat->name;?><a href="<?php echo preg_replace('#&make=[^&]*#', '', $url)?>">[X]</a>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'bedroom' && !empty($facet) && count($facet)>0 && !empty($get['small_category_list'])):?>
                    <div class="link">
                    <label>厅室</label>
                    <?php $bedrooms = Model_Listing_Residential::getBedroomPostAd();?>
                    <?php if(empty($get['bedroom'])):?>
                        <?php ksort($facet);?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = isset($bedrooms[$id]) ? Ac_Util::getChineseNumber($bedrooms[$id]) : '';?>
                            <?php if(!empty($cat)): ?>
                                <a href="<?php echo preg_replace('#&bedroom=[^&]*#', '', $url) . $separator . 'bedroom=' . $id;?>"><?php echo $cat;?>(<?php echo $num?>)</a>
                            <?php endif;?>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $id = $get['bedroom']?>
                        <?php $cat = isset($bedrooms[$id]) ? Ac_Util::getChineseNumber($bedrooms[$id]) : '';?>
                        <?php if(!empty($cat)): ?>
                            <span><?php echo $cat;?><a href="<?php echo preg_replace('#&bedroom=[^&]*#', '', $url)?>">[X]</a>
                        <?php endif;?>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'salary' && !empty($facet) && count($facet)>0 && !empty($get['small_category_list'])):?>
                    <?php $disable_price = true;?>
                    <div class="link">
                    <label>月薪</label>
                    <?php $salarys = Model_Listing_Job::getSalaryPostAd();?>
                    <?php if(empty($get['salary'])):?>
                        <?php ksort($facet);?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = isset($salarys[$id]) ? $salarys[$id] : '';?>
                            <?php if(!empty($cat)): ?>
                                <a href="<?php echo preg_replace('#&salary=[^&]*#', '', $url) . $separator . 'salary=' . $id;?>"><?php echo $cat;?>(<?php echo $num?>)</a>
                            <?php endif;?>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $id = $get['salary']?>
                        <?php $cat = isset($salarys[$id]) ? $salarys[$id] : '';?>
                        <?php if(!empty($cat)): ?>
                            <span><?php echo $cat;?><a href="<?php echo preg_replace('#&salary=[^&]*#', '', $url)?>">[X]</a>
                        <?php endif;?>
                    <?php endif;?>
                    </div>
                <?php endif;?>

                <?php if ($name == 'salary_hour' && !empty($facet) && count($facet)>0 && !empty($get['small_category_list'])):?>
                    <?php $disable_price = true;?>
                    <div class="link">
                    <label>时薪</label>
                    <?php $salarys = Model_Listing_Job::getSalaryHourPostAd();?>
                    <?php if(empty($get['salary_hour'])):?>
                        <?php ksort($facet);?>
                        <?php foreach($facet as $id => $num):?>
                            <?php $cat = isset($salarys[$id]) ? $salarys[$id] : '';?>
                            <?php if(!empty($cat)): ?>
                                <a href="<?php echo preg_replace('#&salary_hour=[^&]*#', '', $url) . $separator . 'salary_hour=' . $id;?>"><?php echo $cat;?>(<?php echo $num?>)</a>
                            <?php endif;?>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php $id = $get['salary_hour']?>
                        <?php $cat = isset($salarys[$id]) ? $salarys[$id] : '';?>
                        <?php if(!empty($cat)): ?>
                            <span><?php echo $cat;?><a href="<?php echo preg_replace('#&salary_hour=[^&]*#', '', $url)?>">[X]</a>
                        <?php endif;?>
                    <?php endif;?>
                    </div>
                <?php endif;?>
        <?php endforeach;?>

        <?php if(!empty($get['small_category_list'])):?>
            <?php foreach($this->facet_queries as $name => $array):?> 
                <?php if (!$disable_price && ($name == 'price' || $name == 'sale_price' || $name ==  'rent_price')):?>
                    <div class="link">
                    <label><?php
                    if ($name == 'price') {
                        $label = '价格';
                    } elseif($name == 'rent_price') {
                        $label = '租';
                    } elseif($name == 'sale_price') {
                        $label = '售';
                    }
                    echo $label;
                    ?></label>
                    <?php foreach($array as $k => $ele):?>
                        <?php
                            if ($ele['count'] == 0) {
                                continue;
                            }
                            $val  = str_replace(array('[', ']'), '', $ele['range']);
                            $tmp2 = explode('TO', $val);
                            $tmp2[0] = trim($tmp2[0]);

                            $price = (!is_numeric(trim($tmp2[0])) ? '' : $tmp2[0]);
                            $price1 = (!is_numeric($tmp2[1]) ? '*' : $tmp2[1]);

                            $tmp2[0] = !is_numeric($tmp2[0]) ? 0 : $tmp2[0];
                            $tmp2[1] = !is_numeric($tmp2[1]) ? '*' : $tmp2[1];


                            if ($tmp2[1] == '*') {
                                $string = Ac_Util::abbreviation(intval($tmp2[0])).'元以上';
                            } else {
                                $tmp2[0] = !is_numeric($tmp2[0]) ? 0 : Ac_Util::abbreviation($tmp2[0]);
                                $tmp2[1] = !is_numeric($tmp2[1]) ? '*' : Ac_Util::abbreviation($tmp2[1]);
                                $string = $tmp2[0] .($tmp2[0]>0 ? '元' : '0元') . ' - '. $tmp2[1].'元';
                            }

                            $get['price'] = empty($get['price']) ? '' : $get['price'];
/*
                            var_dump(
                                $get['price'], $price,
                                empty($get['price']),
                                 empty($get['price-1']),
                                        $get['price'] != $price,
                                        $get['price-1'] != $price1                            
);
*/
                        ?>
                        <?php if($name == 'sold'):?>
                            <span><?php echo 'Sold'?>(<?php echo $num?>)</span>
                        <?php elseif (   
                                        empty($get['price-1']) || 
                                        $get['price'] != $price || 
                                        $get['price-1'] != $price1):?>                                        
                            <a href="<?php echo preg_replace('#&price(.*?)=[^&]*#', '', $url) . $separator . 'price=' . $price .'&price-1=' . $price1;?>"><?php echo $string;?> (<?php echo $ele['count']?>)</a>
                        <?php else:?>
                                <?php echo $string?>
                                <a href="<?php echo preg_replace('#&price(-1)=[^&]*#', '', $url)?>">[X]</a>
                            <?php break;?>
                        <?php endif;?>
                    <?php endforeach;?>
                    </div>
                <?php endif;?>
            <?php endforeach;?>
            <?php //var_dump($this->facet_counts);exit;?>
        <?php endif;?>
    </fieldset>
</form>

